package com.open.capacity.sign.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Daily;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.sign.service.DailyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/daily")
@Api(tags = "SIGN API")
public class DailyController {
    @Autowired
    private DailyService dailyService;

    /**
     *日报提交
     * @param daily
     * @return
     * @throws ControllerException
     */
    @PostMapping("/dailySubmitOrUpdate")
    @ApiOperation(value = "日报提交")
    @LogAnnotation(module = "signin-center")
    public Result dailySubmitOrUpdate(@RequestBody Daily daily) throws ControllerException {
        try {
            return dailyService.dailySubmitOrUpdate(daily);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }


    /**
     * 日报查询
     * @param params
     * @return
     * @throws ControllerException
     */
    @PostMapping("/dailyManage")
    @ApiOperation(value = "日报查询")
    @LogAnnotation(module = "signin-center")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    public PageResult<Daily> dailyManage(@RequestParam Map<String, Object> params) throws ControllerException{
        try {
            return dailyService.dailyManage(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 导出数据
     *
     * @return
     * @throws ControllerException
     */
    @PostMapping("/exportDaily")
//    @PreAuthorize("hasAuthority('user:post/users/exportDaily')")
    public void exportUser(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) throws ControllerException {
        try {
            List<Daily> result = dailyService.findAllDailys(params);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
            @Cleanup OutputStream ouputStream = null;
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("日报导出", "日报"),
                    Daily.class, result);
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        } catch (IOException e) {
            throw new ControllerException(e);
        }

    }

    @PostMapping("/dailyDelete")
    @ApiOperation(value = "日报提交")
    @LogAnnotation(module = "signin-center")
    public Result dailyDelete(@RequestBody Daily daily) throws ControllerException {
        try {
            return dailyService.dailyDelete(daily);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }
}
